//
//  MobileLogin.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/1/26.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "MobileLogin.h"
#import "UIAlertView+BlocksKit.h"
#import "LoginModel.h"

@implementation MobileLogin
{
    int Countdown;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    //手机号输入框
    self.backgroundColor = [UIColor clearColor];
    self.mobile.layer.borderColor = KGray2Color.CGColor;
    self.mobile.backgroundColor = [UIColor clearColor];
    self.mobile.layer.borderWidth = 1.1;
    self.mobile.layer.cornerRadius = 20;
    self.mobile.placeholder = ASLocalizedString(@"输入手机号");
    self.mobile.placeholderColor = KGray2Color;

    self.mobile.borderStyle = UITextBorderStyleNone;
    self.mobile.layer.masksToBounds = YES;
    self.mobile.keyboardType = UIKeyboardTypeNumberPad;
    self.mobile.textColor = KGray2Color;
    //验证码输入框
    self.txtCode.layer.borderColor = KGray2Color.CGColor;
    self.txtCode.backgroundColor = [UIColor clearColor];
    self.txtCode.layer.cornerRadius = 20;
    self.txtCode.placeholder = ASLocalizedString(@"输入验证码");
    self.txtCode.placeholderColor = KGray2Color;
    self.txtCode.layer.masksToBounds = YES;
    self.txtCode.layer.borderWidth = 1.1;
    self.txtCode.layer.cornerRadius = 20;
    self.txtCode.textColor = KGray2Color;
    self.txtCode.keyboardType = UIKeyboardTypeNumberPad;

    self.sendCode.alpha = 0.8;
    self.sendCode.layer.masksToBounds = YES;
    [self.sendCode setBackgroundColor:MainColor];
    
    self.btnLogin.alpha = 0.8;
    [self.btnLogin setBackgroundColor:MainColor];
    
    [self.sendCode addTarget:self action:@selector(handleSendCodeEvent:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)handleSendCodeEvent:(QMUIFillButton *)button {
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"login_api" A:@"code"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if(!StrValid(self.mobile.text) || _mobile.text.length != 11)
    {
        [MBProgressHUD showTipMessageInWindow:ASLocalizedString(@"请输入正确的手机号")];
        return;
    }
    
    button.enabled = NO;
    button.backgroundColor = [UIColor colorWithHexString:@"A69595"];
    Countdown = 60;
    
    [param setObject:self.mobile.text forKey:@"mobile"];
//    [param setObject:[LoginModel sharedLoginModel].invite_code forKey:@"invite_code"];
    [CYNET POST:url parameters:param responseCache:^(id responseObject){
        
    } success:^(id responseObject) {
        [MBProgressHUD showTopTipMessage:ASLocalizedString(@"发送成功")isWindow:YES];
//        [MBProgressHUD showTipMessageInWindow:ASLocalizedString(@"发送成功")];
        [NSTimer scheduledTimerWithTimeInterval:1 block:^(NSTimer * _Nonnull timer) {
            if(Countdown == 0)
            {
                button.backgroundColor = MainColor;
                button.enabled = YES;
                [button setTitle:[NSString stringWithFormat:ASLocalizedString(@"验证")] forState:UIControlStateNormal];
                [timer invalidate];
            }
            else
            {
                
                [button setTitle:[NSString stringWithFormat:ASLocalizedString(@"%d秒"),Countdown] forState:UIControlStateNormal];
                Countdown--;
            }
        } repeats:YES];
    } failure:^(NSString *error) {
        button.backgroundColor = MainColor;
        button.enabled = YES;
        [button setTitle:[NSString stringWithFormat:ASLocalizedString(@"验证")] forState:UIControlStateNormal];
        [MBProgressHUD showTipMessageInWindow:error];
    } hasCache:NO];

}
- (IBAction)doLogin:(id)sender {
    [self endEditing:YES];
    [self jumpLogin];
//    if(KGlobalVariable.appmodel.is_ios_base == 1)
//    {
//        [UIAlertView bk_showAlertViewWithTitle:ASLocalizedString(@"提示")message:ASLocalizedString(@"我已阅读并充分理解底部隐私政策")cancelButtonTitle:ASLocalizedString(@"取消")otherButtonTitles:@[ASLocalizedString(@"我已同意")] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
//            if(buttonIndex == 1)
//            {
//                [self jumpLogin];
//            }
//        }];
//
//    }else
//    {
//        [self jumpLogin];
//    }
    

//

}

- (void)jumpLogin {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if(!StrValid(self.mobile.text) || _mobile.text.length != 11)
    {
        [MBProgressHUD showTipMessageInWindow:ASLocalizedString(@"请输入正确的手机号")];
        return;
    }
    
    if(!StrValid(self.txtCode.text))
    {
        [MBProgressHUD showTipMessageInWindow:ASLocalizedString(@"请输入正确的验证码")];
        return;
    }
    
    [param setObject:self.mobile.text forKey:@"mobile"];
    [param setObject:self.txtCode.text forKey:@"code"];
    
    [[HUDHelper sharedInstance] syncLoading:ASLocalizedString(@"登录中")];
    [userManager login:kUserLoginTypePwd params:param completion:^(BOOL success, NSString *des) {
        if(success)
        {
            [[HUDHelper sharedInstance] syncStopLoading];
            if([curUser.is_reg_perfect isEqualToString:@"0"])
            {
                //填充资料
                if(self.loginHasFillData)
                {
                    self.loginHasFillData();
                }
            }
            else
            {
                KPostNotification(KNotificationLoginStateChange, @YES);
            }
        }
        else
        {
            
        }
        
    }];
}

-(void)getCode
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if(!StrValid(self.mobile.text) || _mobile.text.length != 11)
    {
        [MBProgressHUD showTipMessageInWindow:ASLocalizedString(@"请输入正确的手机号")];
        return;
    }
    
    if(!StrValid(self.txtCode.text))
    {
        [MBProgressHUD showTipMessageInWindow:ASLocalizedString(@"请输入正确的验证码")];
        return;
    }
    
    [param setObject:self.mobile.text forKey:@"mobile"];
    [param setObject:self.txtCode.text forKey:@"code"];
    
    
    [userManager login:kUserLoginTypePwd params:param completion:^(BOOL success, NSString *des) {
        if(success)
        {
            if([curUser.is_reg_perfect isEqualToString:@"0"])
            {
                //填充资料
                if(self.loginHasFillData)
                {
                    self.loginHasFillData();
                }
            }
            else
            {
                KPostNotification(KNotificationLoginStateChange, @YES);
            }
        }
        else
        {
            
        }
        
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
